<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>购物车</title>
    <link rel="stylesheet" type="text/css" href="cart.css">
    <script src="vue-2.5.17.js"></script>
</head>

<body>

    <!--购物车商品table-->
    <div id="app">
        <div class="shoppingBar">
            <h2>购物清单</h2>
            <table>
                <tbody>
                    <tr>
                        <th><input type="checkbox" title="全选" class="selectAll"></th>
                        <th>商品名称</th>
                        <th>数量</th>
                        <th>单价（元）</th>
                        <th>金额（元）</th>
                        <th>操作</th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input" value="1"></th>
                        <th class="content"><img src="img/20201102150701.png" width="100px" height="100px">
                            <p>电视机 </p>
                        </th>
                        <th><button type="button">
                                -
                            </button> <span>
                                1
                            </span> <button type="button">
                                +
                            </button></th>
                        <th>
                            200
                        </th>
                        <th class="money">
                            200
                        </th>
                        <th><span class="glyphicon glyphicon-remove">删除
                            </span></th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input" value="2"></th>
                        <th class="content"><img src="img/20201102150701.png" width="100px" height="100px">
                            <p>洗衣机 </p>
                        </th>
                        <th><button type="button">
                                -
                            </button> <span>
                                1
                            </span> <button type="button">
                                +
                            </button></th>
                        <th>
                            150
                        </th>
                        <th class="money">
                            150
                        </th>
                        <th><span class="glyphicon glyphicon-remove">删除
                            </span></th>
                    </tr>
                    <tr class="commodity">
                        <th><input type="checkbox" class="input" value="3"></th>
                        <th class="content"><img src="img/20201102150701.png" width="100px" height="100px">
                            <p>冰箱 </p>
                        </th>
                        <th><button type="button">
                                -
                            </button> <span>
                                1
                            </span> <button type="button">
                                +
                            </button></th>
                        <th>
                            100
                        </th>
                        <th class="money">
                            100
                        </th>
                        <th><span class="glyphicon glyphicon-remove">删除
                            </span></th>
                    </tr>
                </tbody>
            </table>
            <div class="shoppingBar-footer">
                <div class="manage">
                    <span class="delAll">
                        删除所选商品
                    </span>
                    <span class="return">
                        继续购物
                    </span>
                </div>
                <button id="go" type="button">
                    去结算
                </button>
                <div class="buy">
                    <span>
                        0
                    </span>
                    件商品总计:
                    <span>
                        ￥{{total}}
                    </span>
                </div>
            </div>
        </div>
    </div>


</body>

</html>
<script>
    /*
        1、加减按钮 点击 导致金额改变
        2、当勾选 某一个商品之后 需要计算 总金额
        3、当删除 某一个商品之后  需要计算总金额
    */

    // 委托绑定点击事件
    document.querySelector('.shoppingBar').addEventListener('click', function (event) {
        if (event.target.nodeName == 'BUTTON') {
            if (event.target.innerHTML.trim() == '-') {
                // 表示减少
                // 获取当前数量
                let number = event.target.nextElementSibling.innerHTML.trim();
                if (number <= 1) {
                    alert('商品最少一件')
                    return;
                }
                number--;
                // 修改数量
                event.target.nextElementSibling.innerHTML = number;
                // 修改小计金额
                let price = event.target.parentElement.nextElementSibling.innerHTML.trim() - 0;
                event.target.parentElement.nextElementSibling.nextElementSibling.innerHTML = price * number;
                getTotal();
            } else {
                // 增加数量
                // 获取当前数量
                let number = event.target.previousElementSibling.innerHTML.trim();
                number++;
                // 修改数量
                event.target.previousElementSibling.innerHTML = number;
                // 修改小计金额
                let price = event.target.parentElement.nextElementSibling.innerHTML.trim() - 0;
                event.target.parentElement.nextElementSibling.nextElementSibling.innerHTML = price * number;
                getTotal();
            }
        }else if(event.target.nodeName == 'INPUT') {
            document.querySelector('.selectAll').checked = document.querySelectorAll('.input').length == document.querySelectorAll('.input:checked').length
            getTotal();
        }
        else if(event.target.classList.contains('glyphicon-remove')){
          
            document.querySelector('tbody').removeChild(event.target.parentElement.parentElement);
            getTotal();
        }
    })

    //计算所有被勾选商品的总金额是多少 
    function getTotal() {
        let total = 0;
        // 根据查找的被勾选的input进行遍历
        document.querySelectorAll('.input:checked').forEach(dom => {
            total += Number(dom.parentElement.nextElementSibling.nextElementSibling.nextElementSibling
                .nextElementSibling.innerHTML.trim());
        })
        document.querySelectorAll('.buy>span')[1].innerHTML = ' ￥' + total
    }
</script>